<template>
  <div>
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item>
        <el-input
          v-model="params.agency_name"
          clearable
          @keyup.enter.native="getData()"
          placeholder="机构名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()" class="btn-long" :loading="loading">查询</el-button>
      </el-form-item>
    </el-form>
    <c-table border class="table-sm" v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:customer="scope">
        <span v-if="!scope.row.total">-</span>
        <el-button v-else type="text" @click="handleDetail(scope.row)">
          {{ scope.row.total }}
        </el-button>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <bank-in-detail :visible.sync="visible" :bank-id="bankId"></bank-in-detail>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import BankInDetail from '@/credit/views/org/BankInDetail'
export default {
  components: {
    BankInDetail,
  },
  data() {
    return {
      params: {
        index: 1,
        size: 15,
        isBIQuery: 1,
      },
      order: {
        // total: 'desc'
      },
      loading: false,
      tableName: 'credit_agency_total_bank',
      tableData: [],
      tableColumns: [
        { attrs: { label: '机构名称', prop: 'agency_name' } },
        { attrs: { label: '项目数量', prop: 'total', align: 'center' }, slot: 'customer' },
        { attrs: { label: '在保余额', prop: 'funds', align: 'center' }, process: v => v || '-' },
        {
          attrs: { label: '授信金额', prop: 'credit_amount', align: 'center' },
          process: v => v || '-',
        },
        {
          attrs: { label: '代偿金额', prop: 'comps_funds', align: 'center' },
          process: v => v || '-',
        },
      ],
      totalCount: 0,
      visible: false,
      bankId: '',
    }
  },

  computed: {
    ...mapGetters(['clientId']),
  },

  created() {
    this.params['service_id.eq'] = this.clientId
    this.getData()
  },
  methods: {
    getData(index = 1) {
      // this.getCount()
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = data.data || []
          this.totalCount = data.totalCount
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleDetail(row) {
      this.bankId = row.bank_id
      this.visible = true
    },
    getCount() {
      const { index, size, ...params } = this.params
      const sumKeys = ['total', 'funds', 'credit_amount', 'comps_funds']
      const groupFunc = sumKeys
        .reduce((a, b) => {
          a.push(`round(sum(${b})|2)`)
          return a
        }, [])
        .join(',')
      params.groupFunc = groupFunc
      this.$api.getData(this.$serviceName, this.tableName, params).then(res => {
        const data = (res.data && res.data[0]) || {}
        this.tableColumns[1].attrs.label = `项目数量(${data.p || '-'})`
        this.tableColumns[2].attrs.label = `在保余额(${data.p1 || '-'} 万元)`
        this.tableColumns[3].attrs.label = `授信金额(${data.p2 || '-'} 万元)`
      })
    },
  },
}
</script>
<style lang="scss" scoped></style>
